﻿@inject IHttpService _httpService
@inject NavigationManager NavigationManager
@inject IDataCacheService _dataCacheService
@inject IPageModelCatche<List<FavoriteFolderOverviewModel>> _pageModelCatche

@if (_dataCacheService.SpaceViewCache.ViewOnFavoriteFolderId != 0)
{
    <FavoriteObjectsViewTip OnReturn="OnReturn" Title="@_dataCacheService.SpaceViewCache.ViewOnFavoriteFolderName" Id="_dataCacheService.SpaceViewCache.ViewOnFavoriteFolderId" IsCurrentUser="IsCurrentUser"></FavoriteObjectsViewTip>
}
else
{
    <CnGalWebSite.Components.Containers.MasaTitleContainer Title="收藏夹" Icon="@IconType.Favorite.ToIconString()" Class="mb-4" Outline>
        <ButtonContent>
            @if (IsCurrentUser)
            {
                <CnGalWebSite.Components.Buttons.MasaButton Rounded Text="编辑" Icon="mdi-pencil" OnClick="OnManage" />

            }
        </ButtonContent>
    </CnGalWebSite.Components.Containers.MasaTitleContainer>
    @if (Model.Any())
    {
        foreach (var item in Model)
        {
            <FavoriteFolderInforViewTip Model="item" OnClickLayout="OnClickFolder"></FavoriteFolderInforViewTip>
        }
    }
}

@code {
    public List<FavoriteFolderOverviewModel> Model { get; set; } = new List<FavoriteFolderOverviewModel>();

    public PagedSortedAndFilterInput FavoriteObjectInput { get; set; } = new PagedSortedAndFilterInput();

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    [Parameter]
    public string UserId { get; set; }
    [Parameter]
    public bool IsCurrentUser { get; set; }

    public async Task Refresh(string id)
    {
        UserId = id;
        await OnInitializedAsync();
    }

    protected override async Task OnInitializedAsync()
    {
        try
        {
            if (UserId != _dataCacheService.SpaceViewCache.UserId)
            {
                _dataCacheService.SpaceViewCache.UserId = UserId;
                _dataCacheService.SpaceViewCache.ViewOnFavoriteFolderId = 0;
            }

            if (string.IsNullOrWhiteSpace(UserId))
            {
                return;
            }
            Model = await _pageModelCatche.GetCache("api/favorites/GetUserFavoriteFolders/"+UserId);

            StateHasChanged();
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取收藏夹列表失败");
        }
    }

    public void OnClickFolder(FavoriteFolderOverviewModel model)
    {
        _dataCacheService.SpaceViewCache.ViewOnFavoriteFolderId = model.Id;
        _dataCacheService.SpaceViewCache.ViewOnFavoriteFolderName = model.Name;
        StateHasChanged();
    }

    public void OnReturn()
    {
        _dataCacheService.SpaceViewCache.ViewOnFavoriteFolderId = 0;
        _dataCacheService.SpaceViewCache.TabIndex = 11;
        NavigationManager.NavigateTo("/space/index/" + UserId);
        StateHasChanged();
    }
    public void OnManage()
    {
        NavigationManager.NavigateTo("/space/EditSpaceFavoriteFolders/" + UserId);
        
    }
}
